package org.top10.parser;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.top10.entity.CollectEntity;
import org.top10.entity.Reading;
import org.top10.executor.ParseSource;
import org.top10.util.NdcFunction;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class SohuTask extends Task{
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_SOHU;// 对应网站ID
	private static final String ENCoding =TOP10Global.Encoding_GDK;// 对应网站ID
	
	public SohuTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new BookAllTypeParser());
		PARSER.add(new BookOriginalParser());
		PARSER.add(new SohuParser("国内新闻排行",TOP10Global.TYPE_News,"国内新闻日点击排行"));
		PARSER.add(new SohuParser("国际新闻排行",TOP10Global.TYPE_News,"国际新闻日点击排行"));
		PARSER.add(new SohuParser("社会新闻排行",TOP10Global.TYPE_News,"社会新闻日点击排行"));
		PARSER.add(new SohuParser("体育排行",TOP10Global.TYPE_Sports,"体育新闻日点击排行"));
		PARSER.add(new SohuParser("娱乐排行",TOP10Global.TYPE_ENT,"娱乐新闻日点击排行"));
		PARSER.add(new SohuParser("财经排行",TOP10Global.TYPE_Finance,"财经新闻日点击排行"));
	}
	public static void main(String arg[]) throws Exception{
		//new BookAllTypeParser().dealText(ENCoding);
		//new SohuEmotionParser().dealText(TOP10Global.Encoding_GDK);
		System.out.println(URLDecoder.decode(new String("&#28418;&#22312;&#28023;&#22806;&#28404;&#33609;&#40481;&#65292;&#20964;&#20976;&#65292;JP......")));
	}
	
}
abstract class SohuBookParser extends TOPParserImpl {
	
	
	public SohuBookParser(String t,String url) {
		super(t,TOP10Global.TYPE_Reading, url);
	}
	public abstract List<CollectEntity> parse(ParseSource doc) throws Exception ;

	public abstract CollectEntity build(Node node, int rank) throws Exception ;
		// TODO Auto-generated method stub
	public  List parse1(ParseSource doc,String sign,String tag) throws Exception{
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		//Node node = getNodeByText(body, "table",sign);
		Node node =getNodeByAttr("table","id",sign,body);
		ArrayList<Node> trs = new ArrayList<Node>();
		getAllTagsByParent(node,tag, trs);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		int rank=1;
		for (int i = 0; i <trs.size(); i++) {
			Node tr=trs.get(i);
			CollectEntity v =build(tr,rank);
			if(v!=null){
				System.out.println(v);
				list.add(v);
				rank++;
				if(rank>10)break;
			}
		}
		return list;
	}

	public  CollectEntity build1(Node node, int rank,String baseUrl) throws Exception{
		Reading v=new Reading();
		v.setRank(rank);
		List<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		v.setName(as.get(0).getTextContent());
		String url=getAttrValueByTag(as.get(0),"href");
		v.setLinkUrl(baseUrl+url);
		return v ;
	}
}
class BookAllTypeParser extends SohuBookParser{
	
	private static final String TITLE="读书排行";
	private static final String URL="http://lz.book.sohu.com/lz_top.php?ptp=0&stp=0&ctp=1&page=1&pagesize=10&px=0&ttp=1";
	private static final String BASE_URL="http://lz.book.sohu.com/";
	private static final String SIGN="list";
	public BookAllTypeParser() {
		super(TITLE, URL);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		return build1(node,rank,BASE_URL);
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		return  parse1(doc,SIGN,"span");
	}
	
}

class BookOriginalParser extends SohuBookParser{
	
	private static final String TITLE="原创排行";
	private static final String URL="http://yc.sohu.com/book_top.php?ptp=0&stp=0&ttp=1&page=1&ctp=1";
	private static final String BASE_URL="http://yc.sohu.com/";
	private static final String SIGN="tt";
	public BookOriginalParser() {
		super(TITLE, URL);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		
		String url=getAttrValueByTag(node,"href");
		if(url!=null&&url.indexOf("bookinfo-")!=-1){
			Reading v=new Reading();
			v.setRank(rank);
			v.setName(node.getTextContent());
			v.setLinkUrl(BASE_URL+url);
			return v ;
		}
		return null;
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		return  parse1(doc,SIGN,"a");
	}
}
 class SohuParser extends TOPParserImpl{
	 private static final String URL="http://comment.news.sohu.com/djpm/";
	 private String sign;
	public SohuParser(String c, int t,String sign) {
		super(c, t, URL);
		this.sign=sign;
	}
	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		Node node = getNodeByText(body, "h2",sign).getParentNode().getParentNode().getNextSibling().getNextSibling();
		System.out.println(node.getTextContent());
		ArrayList<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		int rank=1;
		for (int i = 0; i < as.size(); i++) {
			Node tr=as.get(i);
			CollectEntity v =build(tr,rank);
			if(v!=null){
				list.add(v);
				System.out.println(v);
				rank++;
				if(rank>10)break;
			}
		}
		return list;
	}
	
	public CollectEntity build(Node node, int rank) throws Exception {
		String url=getAttrValueByTag(node,"href");
		if(url!=null&&!url.startsWith("http://comment")){
			CollectEntity ce=TOP10Global.getCollectEntityByType(this.getType());
			ce.setName(node.getTextContent());
			ce.setRank(rank);
			ce.setLinkUrl(url);
			return ce;
		}
		return null;
	}
	
}
// class SohuEmotionParser extends TOPParserImpl {
//	 private static final String SIGN="qgbg08.gif";
//	public SohuEmotionParser() {
//		super("情感排行",TOP10Global.TYPE_Emotion,"http://bbs.sohu.com/qinggan/");
//	}
//
//	public List<CollectEntity> parse(ParseSource doc) throws Exception {
//		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
////		Node node = getNodeByText(body, "span","情感社区");
//		//Node node =getNodeByAttr("img","src",SIGN,body).getParentNode().getParentNode();
//		System.out.println(doc.getTextSource());
//		return null;
//	}
//
//	public CollectEntity build(Node node, int rank) throws Exception {
//		return null;
//	}
//	 
// }